//#include <config.h>
//#include <gnome.h>
#include <gtk/gtk.h>
#include <sys/stat.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <errno.h>
#include <popt.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "globals.h"
#include "holyshield.h"
#include "gui.h"
//#include "menus.h"
#include "util.h"
#include "logread.h"
//#include "wizard.h"
#include "preferences.h"
#include "scriptwriter.h"
//#include "dhcp-server.h"
#include "statusview.h"

HolyshieldApp holyshield;
//GTK+中文显示支持
char *_(char *string)
{
    return g_convert(string,-1,"UTF-8","GB18030",NULL,NULL,NULL);;
}

/* [ start_firewall ]
 * Executes the firewall script
 */
void
start_firewall (void)
{
        	gint retval;
        	gchar *arg[3] = {"holyshield.sh", "start", NULL};
        	gchar *output;
        	GError *error = NULL;

        	if (g_spawn_sync (HOLYSHIELD_RULES_DIR"/script",
        	                  arg, NULL,
        	                  G_SPAWN_STDERR_TO_DEV_NULL,
        	                  NULL, NULL,
        	                  &output, // Standard output
        	                  NULL, // Standard error 
        	                  &retval, &error) != TRUE) {
        		printf ("Error spawning shell process: %s\n", error->message);
        	}

        	printf ("%s", output);

        	if (retval == 0) {
        		if (!CONSOLE)
        			status_set_state (STATUS_RUNNING);
        	} else {
        		gchar *message;
        		retval = WEXITSTATUS (retval);

        		if (retval == RETURN_EXT_FAILED) {
        			message = g_strdup_printf (_(
        				"The device %s is not ready."), 
        				preferences_get_string (PREFS_FW_EXT_IF));
        		} else if (retval == RETURN_INT_FAILED) {
        			message = g_strdup_printf (_(
        				"The device %s is not ready."), 
        				preferences_get_string (PREFS_FW_INT_IF));
        		} else if (retval == RETURN_NO_IPTABLES) {
        			message = g_strdup (_("Your kernel does not support iptables."));
        		} else {
        			message = g_strdup (_("An unknown error occurred."));
        		}

        		message = g_strconcat (message, "\n\n", _(
        			"Please check your network device settings and make sure your\n"
        			"Internet connection is active."), NULL);
        		
        		if (CONSOLE) {
        			message = g_strconcat (_("Failed to start the firewall\n"),
        			                       message, NULL);
        			show_error (message);
        		} else {
        			error_dialog (_("Failed to start the firewall"),
        			              _("Failed to start the firewall"),
        				      message,
        				      holyshield.window);
        			status_set_state (STATUS_STOPPED);
        		}

        		g_free (message);
        	}

        	g_free (output);
}

void
restart_firewall_if_active (void)
{
	if (status_get_state () == STATUS_RUNNING ||
	    status_get_state () == STATUS_HIT)
			start_firewall ();
}

/* [ exit_firestarter ]
 * Quit firestater
 */
void
exit_holyshield (void)
{
        gtk_main_quit ();
}

static const gchar *
get_lock_file_path (void)
{
	static gchar *path = NULL;

	if (path == NULL) {
		DIR *d;

		if ((d = opendir ("/var/lock/subsys")) != NULL) {
			closedir (d);
			path = g_strdup ("/var/lock/subsys/firestarter");
		} else if ((d = opendir ("/var/lock")) != NULL) {
			closedir (d);
			path = g_strdup ("/var/lock/firestarter");
		} else {
			perror ("Not able to determine a lock file");
		}
	}

	return path;
}
gboolean
holyshield_is_locked (void)
{
        return g_file_test (get_lock_file_path (), G_FILE_TEST_EXISTS);
}

static gboolean
is_root (void)
{
        if (getuid () != 0) {
                error_dialog ("错误",
                              "权限不够！",
                              "圣盾防火墙需要root权限运行。",
                              holyshield.window);

                return FALSE;
        }
        return TRUE;
}

/* [ main ]
 * The main function, this is where it all begins and ends
 */
int
main (int argc, char* argv[])
{
        //每一个gtk程序都会用到这一句，用来初始化
        gtk_init(&argc, &argv);

        /* Check that the user is root */
        if (!is_root ())
                return 1;

        /* Creating the GUI */
        gui_construct ();
        /* Attach a timeout that keeps the GUI fw status in sync with userland changes */
	status_sync_timeout (NULL); /* Do one immediate refresh */
        g_timeout_add (5000, status_sync_timeout, NULL);

        /* Initialize the system log file polling function */
        //open_logfile ((gchar *)get_system_log_path ());

        //policyview_install_default_ruleset ();

        /* Test that our scripts are up to date */
        /*	if (!scriptwriter_versions_match ()) {*/
        /*		printf (_("Updating firewall to new version...\n"));*/
        /*		scriptwriter_output_scripts ();*/
        /*		printf (_("Firewall update complete\n"));*/
        /*	}*/
	policyview_install_default_ruleset();
	scriptwriter_output_scripts();
        start_firewall ();

        gui_set_visibility (TRUE);

        /*将window内所有的组件都显示出来，这们我们才能看见*/
//      gtk_widget_show_all(holyshield.window);

        /*这也是每一个gtk程序都要有的*/
#ifdef DEBUG
printf("\n***holyshield.c: main(): --> gtk_main():\n");
#endif
        gtk_main();

        return 0;
}

